<?php
/**
 * Nordic Repository
 * 
 * LICENSE
 * 
 * The new BSD license is applied on this source-file. For further
 * information please visit http://license.nordic-dev.de/newbsd.txt
 * or send an email to andre.moelle@gmail.com.
 */

/**
 * Necessary includes.
 */
Zend_Loader::loadClass('Zend_Controller_Action');

/**
 * Default implementation for the controller.
 * 
 * @category   Nordic
 * @package    Nordic_Controller
 * @copyright  2007 Nordic Development
 * @license    http://license.nordic-dev.de/newbsd.txt (New-BSD license)
 * @author     Andre Moelle <andre.moelle@gmail.com>
 * @version    $Id: Action.php 3 2007-07-08 09:21:42Z andre.moelle $
 */
class Nordic_Controller_Action extends Zend_Controller_Action
{
	/**
	 * Name of the template to be rendered by the View-object.
	 * 
	 * @var string
	 */
	protected $_template;
	
	/**
	 * Returns the name of the controller.
	 * 
	 * @param void
	 * @return string
	 */
	public function getControllerName ()
	{
		static $_name;
		
		if($_name == null)
		{
			$refl = new ReflectionObject($this);
			
			$_name = substr(
				$refl->getName(),
				0,
				strpos($refl->getName(), 'Controller')
			);
		}
		
		return $_name;
	}
	
	/**
	 * Returns the name of the template which should be rendered.
	 * 
	 * @param void
	 * @return string
	 */
	public function getTemplate ()
	{
		return $this->_template;
	}
	
	/**
	 * Returns an instance of the view-method.
	 * 
	 * @param void
	 * @return Zend_View_Abstract
	 */
	public function getView ()
	{
		static $view = null;
		
		if($view == null)
		{
			$refl = new ReflectionObject($this);
			
			$view = Nordic_View_Factory::getView();
			$view->setScriptPath(
				NORDIC_VIEW . $this->getControllerName() . DIRECTORY_SEPARATOR
			);
			$view->addHelperPath(
				NORDIC_VIEW . $this->getControllerName() . DIRECTORY_SEPARATOR . 'helpers',
				$this->getControllerName() . '_Helper_'
			);
		}
		
		return $view;
	}
	
	/**
	 * This method is executed after the action-method.
	 * 
	 * It renders the template if any was determined.
	 * 
	 * @param void
	 * @return void
	 */
	public function postDispatch ()
	{
		if($this->getTemplate())
		{
			$this->getResponse()
				 ->appendBody($this->getView()->render($this->getTemplate()));
		}
	}
	
	/**
	 * Sets a new template.
	 * 
	 * @param string $name name of the template
	 * @return void
	 */
	public function setTemplate ($name)
	{
		$this->_template = $name;
	}
}
?>